package com.xsoft.homework8;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.DecimalFormat;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ShopsListActivity extends AppCompatActivity implements OnPriceListener {
    private TextView function;
    private RecyclerView recyclerView;
    private TextView count;
    private TextView price;
    private Button delete;
    private float totalPrice;
    private int totalCount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_list);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.repeat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchAdapter();
            }
        });
        function = (TextView) findViewById(R.id.function);
        function.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (delete.getVisibility() == View.VISIBLE) {
                    delete.setVisibility(View.GONE);
                    function.setText("管理");
                } else {
                    delete.setVisibility(View.VISIBLE);
                    function.setText("完成");
                }
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        count = (TextView) findViewById(R.id.count);
        price = (TextView) findViewById(R.id.price);
        delete = (Button) findViewById(R.id.delete);
        initListAdapter();
    }

    private void switchAdapter() {
        totalPrice = 0;
        if (recyclerView.getAdapter() instanceof ShopGridAdapter) {
            initListAdapter();
        } else {
            initGridAdapter();
        }
    }

    private void initListAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        ShopsListAdapter adapter = new ShopsListAdapter();
        adapter.setOnPriceListener(this);
        adapter.bindData(new ShopDataBaseHelper(this).query());
        recyclerView.setAdapter(adapter);
    }

    private void initGridAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        ShopGridAdapter adapter = new ShopGridAdapter();
        adapter.setOnPriceListener(this);
        adapter.bindData(new ShopDataBaseHelper(this).query());
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onPrice(float price) {
        totalPrice += price;
        if (price < 0) {
            totalCount--;
        } else if (price > 0) {
            totalCount++;
        }
        this.count.setText("已选个数：" + totalCount);
        this.price.setText("合计：¥" + new DecimalFormat("0.00").format(totalPrice));
    }
}
